package cn.cas.mango.dto.param;

import cn.cas.mango.util.string.StringUtils;
import co.elastic.clients.elasticsearch._types.FieldValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class QueryParam extends Param{
  private Integer pageSize;
  private String queryField;
  private String queryStr;
  private String searchType;
  private String sortType;
  private String type;
  private Double param;
  private boolean zh;

  // 查询ids，英文逗号隔开
  private String ids;
  // ids的array
  private List<FieldValue> idsArray;
  // 相似度指标
  private String likeDegree;

  // es库里的id
  private String _id;
  // es的index
  private String index;

  // keywords or entities
  private String recommendType;
  private List<Map<String, Object>> keywordsWithScore= new ArrayList<>();



  public static QueryParam getFromFormData(Map<String, Object> errMessage) {
    QueryParam queryParam = (QueryParam) errMessage.get("formData");
    queryParam.setQueryStr(
        StringUtils.preprocessString(queryParam.getQueryStr())
    );
    return queryParam;
  }

  /**
   * @return 由 order sortField SortOptions 组成的列表
   */
  public List<Object> get3Sorts() {
    SortOrder order;
    String sortField;
    switch (this.getSortType()) {
      case "timeasc":
        order = SortOrder.Asc;
        sortField = "news_publictime";
        break;
      case "sim":
        order = SortOrder.Desc;
        sortField = "_score";
        break;
      default:
        order = SortOrder.Desc;
        sortField = "news_publictime";
        break;
    }
    SortOptions sortOptions = SortOptions.of(
        s -> s.field(f -> f.field(sortField).order(order)));
    return new ArrayList<Object>(){{add(order);add(sortField); add(sortOptions);}};
  }
}
